# -*- coding:utf-8 -*-

import os
import re
from abc import ABCMeta, abstractmethod

from src._items import OCR_MODEL_BASE_PATH

__all__ = ('OCRReader',)


class OCRReader(metaclass=ABCMeta):
    """
    OCR识别器基类
    """

    # 默认支持的OCR识别引擎
    _engine_type: str
    # 默认支持的语言
    _supported_languages: tuple[str, ...]
    # OCR模型路径
    _model_path: str = OCR_MODEL_BASE_PATH
    # 正则表达式，用于过滤特殊字符
    _regex_special = r'[`~!@#$%^&*()+|\':;,\[\].<>/?~！@#￥%……&*（）+|【】‘；：”“’。 ，、？\.]'

    def __init__(self, image_path: str, language: str | list | None = None,
                 model_base_path: str | None = None):
        # OCR识别引擎
        self._engine = None
        if not self._is_image_exists(image_path):
            raise FileNotFoundError(f'当前图片({image_path})不存在！')
        # OCR识别图片路径
        self._image_path = image_path
        # OCR识别语言
        self._language = language
        # 配置模板默认路径
        if model_base_path is not None:
            self._model_path = model_base_path
        # OCR识别全部结果
        self._ocr_full_result: list | None = None

    @property
    def engine(self):
        return self._engine

    @property
    def language(self) -> str:
        return self._language

    @property
    def engine_type(self) -> str:
        return self._engine_type

    @property
    def model_path(self) -> str:
        return self._model_path

    @property
    def ocr_full_result(self) -> list | None:
        return self._ocr_full_result

    @property
    def regex_special(self) -> str:
        return self._regex_special

    @regex_special.setter
    def regex_special(self, regex: str):
        self._regex_special = regex

    @abstractmethod
    def _init_engine(self):
        pass

    @staticmethod
    def _is_image_exists(img_path) -> bool:
        return os.path.exists(img_path)

    def _filter_special_chars(self, text: str) -> str:
        """
        过滤特殊字符
        Args:
            text(str): 待过滤的文本

        Returns:
            res(str): 过滤后的文本

        """
        pattern = re.compile(self.regex_special)
        res = re.sub(pattern, '', text)
        return res

    @abstractmethod
    def _get_result(self):
        pass

    @abstractmethod
    def text_is_match(self, *args, **kwargs) -> bool:
        pass

    @abstractmethod
    def texts_is_all_match(self,  *args, **kwargs) -> bool:
        pass

    @abstractmethod
    def text_match_location(self, *args, **kwargs):
        pass

    @abstractmethod
    def texts_match_location(self, *args, **kwargs):
        pass

# if __name__ == '__main__':
#
#     # ocr_reader = OCRReader('../test_pictures/numbers.jpeg')
#     # print(ocr_reader._filter_special_chars('E.I.G.HT'))
#
#     regex_special = r'[`~!@#$%^&*()+=|{}\':;,\[\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。 ，、？\.]'
#
#     def filter_special_chars(text: str) -> str:
#         """
#         过滤特殊字符
#         Args:
#             text(str): 待过滤的文本
#
#         Returns:
#             res(str): 过滤后的文本
#
#         """
#         pattern = re.compile(regex_special)
#         res = re.sub(pattern, '', text)
#         return res
#
#     print(filter_special_chars('E.I.G.HT'))
